package com.yjhb.android.sbz.tool;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

import java.util.ArrayList;

public
class ShareUtils {


    /**
     * 分享网页类型至微信
     *
     * @param context 上下文
     * @param appId   微信的appId
     * @param target  分享目标 0 分享到会话，1 分享到朋友圈
     * @param webUrl  网页的url
     * @param title   网页标题
     * @param content 网页描述
     * @param bitmap  位图
     */
    public static void WxShareWeb(Context context, String appId, int target, String webUrl, String title, String content, Bitmap bitmap) {
        // 通过appId得到IWXAPI这个对象
        IWXAPI wxapi = WXAPIFactory.createWXAPI(context, appId);
        // 检查手机或者模拟器是否安装了微信
        if (!wxapi.isWXAppInstalled()) {
            Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_SHORT);
            return;
        }

        // 初始化一个WXWebpageObject对象
        WXWebpageObject webpageObject = new WXWebpageObject();
        // 填写网页的url
        webpageObject.webpageUrl = webUrl;

        // 用WXWebpageObject对象初始化一个WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage(webpageObject);
        // 填写网页标题、描述、位图
        msg.title = title;
        msg.description = content;
        // 如果没有位图，可以传null，会显示默认的图片
        msg.setThumbImage(bitmap);

        // 构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        // transaction用于唯一标识一个请求（可自定义）
        req.transaction = "webpage";
        // 上文的WXMediaMessage对象
        req.message = msg;
        // SendMessageToWX.Req.WXSceneSession是分享到好友会话
        // SendMessageToWX.Req.WXSceneTimeline是分享到朋友圈
        if (target == 1) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        }

        // 向微信发送请求
        wxapi.sendReq(req);
    }


    /**
     * 分享图片到微信
     *
     * @param context
     * @param appId
     * @param target  0 发给朋友，1发到朋友圈
     * @param bitmap
     */
    public static void WxShareImage(Context context, String appId, int target, Bitmap bitmap) {
        // 通过appId得到IWXAPI这个对象
        IWXAPI wxapi = WXAPIFactory.createWXAPI(context, appId);
        // 检查手机或者模拟器是否安装了微信
        if (!wxapi.isWXAppInstalled()) {
            Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_SHORT);
            return;
        }
        //创建Wx图片对象
        WXImageObject imageObject = new WXImageObject(bitmap);

        WXMediaMessage mediaMessage = new WXMediaMessage();
        mediaMessage.mediaObject = imageObject;
        mediaMessage.description = "image";

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = mediaMessage;
        if (target == 1) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        }

        // 向微信发送请求
        wxapi.sendReq(req);

    }

    /**
     * QQ分享图片给好友
     * @param activity
     * @param appId
     * @param path
     * @param uiListener
     */
    public static void QQShareImage(Activity activity, String appId, String path, IUiListener uiListener) {
        Tencent tencent = Tencent.createInstance(appId, activity);
        Bundle bundle = new Bundle();
        //设置分享类型为图片类型
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path);
        tencent.shareToQQ(activity, bundle, uiListener);
    }

    /**
     * 分享图文到QQ空间
     * @param activity
     * @param appId
     * @param title  标题
     * @param toUrl  跳转的url
     * @param imgPath  图片路径
     * @param uiListener
     */

    public static void QZoneShare(Activity activity, String appId, String title,
                                  String toUrl, ArrayList<String> imgPath, IUiListener uiListener){
        Tencent tencent = Tencent.createInstance(appId,activity);
        Bundle bundle = new Bundle();
        bundle.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        bundle.putString(QzoneShare.SHARE_TO_QQ_TITLE,title);
        bundle.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,toUrl);
        bundle.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imgPath);
        tencent.shareToQzone(activity,bundle,uiListener);
    }


    /**
     * 分享图文到QQ
     * @param activity
     * @param appId
     * @param title
     * @param toUrl
     * @param content
     * @param imgPath
     * @param uiListener
     */
    public static void QQShareImageText(Activity activity, String appId, String title,
                                         String toUrl, String content,String imgPath, IUiListener uiListener) {
        Tencent tencent = Tencent.createInstance(appId, activity);
        Bundle bundle = new Bundle();
        //设置分享类型为图片类型
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL,toUrl);
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE,title);
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY,content);
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,imgPath);
        tencent.shareToQQ(activity, bundle, uiListener);
    }

}
